/** 
 * æ°ç»å¸®å©ç±»
 * @author kehailin
 * @time 2018/12/8
 */

var arrayUtils = {
		/*å¤æ­ä¸ä¸ªåç´ æ¯å¦å¨æ°ç»ä¸­*/  
		contains:function (arr, val) {  
		    return arr.indexOf(val) != -1 ? true : false;  
		} ,
		 
		/**  
		 * @param  {arr} æ°ç»  
		 * @param  {fn} åè°å½æ°  
		 * @return {undefined}  
		 */  
		each :function(arr, fn) {  
		    fn = fn || Function;  
		    var a = [];  
		    var args = Array.prototype.slice.call(arguments, 1);  
		    for(var i = 0; i < arr.length; i++) {  
		        var res = fn.apply(arr, [arr[i], i].concat(args));  
		        if(res != null) a.push(res);  
		    }  
		} , 
		 
		/**  
		 * @param  {arr} æ°ç»  
		 * @param  {fn} åè°å½æ°  
		 * @param  {thisObj} thisæå  
		 * @return {Array}   
		 */  
		map :function(arr, fn, thisObj) {  
		    var scope = thisObj || window;  
		    var a = [];  
		    for(var i = 0, j = arr.length; i < j; ++i) {  
		        var res = fn.call(scope, arr[i], i, this);  
		        if(res != null) a.push(res);  
		    }  
		    return a;  
		} ,
		 
		/**  
		 * @param  {arr} æ°ç»  
		 * @param  {type} 1ï¼ä»å°å°å¤§   2ï¼ä»å¤§å°å°   3ï¼éæº  
		 * @return {Array}  
		 */  
		sort:function (arr, type) {  
		    return arr.sort( (a, b) => {  
		        switch(type) {  
		            case 1:  
		                return a - b;  
		            case 2:  
		                return b - a;  
		            case 3:  
		                return Math.random() - 0.5;  
		            default:  
		                return arr;  
		        }  
		    })  
		}  , 
		 
		/*å»é*/  
		unique:function (arr) { 
		    if ( Array.hasOwnProperty('from') ) {  
		        return Array.from(new Set(arr));  
		    }else{  
		        var n = {},r=[];   
		        for(var i = 0; i < arr.length; i++){  
		            if (!n[arr[i]]){  
		                n[arr[i]] = true;   
		                r.push(arr[i]);  
		            }  
		        }  
		        return r;  
		    }  
		}  ,
		 
		/*æ±ä¸¤ä¸ªéåçå¹¶é*/  
		union:function (a, b) {  
		    var newArr = a.concat(b);  
		    return this.unique(newArr);  
		} , 
		 
		/*æ±ä¸¤ä¸ªéåçäº¤é*/  
		intersect:function (a, b) {  
		    var _this = this;  
		    a = this.unique(a);  
		    return this.map(a, function(o) {  
		        return _this.contains(b, o) ? o : null;  
		    });  
		} ,
		 
		/*å é¤å¶ä¸­ä¸ä¸ªåç´ */  
		remove:function (arr, ele) {  
		    var index = arr.indexOf(ele);  
		    if(index > -1) {  
		        arr.splice(index, 1);  
		    }  
		    return arr;  
		} ,
		 
		/*å°ç±»æ°ç»è½¬æ¢ä¸ºæ°ç»çæ¹æ³*/  
		formArray:function (ary) {  
		    var arr = [];  
		    if(Array.isArray(ary)) {  
		        arr = ary;  
		    } else {  
		        arr = Array.prototype.slice.call(ary);  
		    };  
		    return arr;  
		} ,
		 
		/*æå¤§å¼*/  
		max:function (arr) {  
		    return Math.max.apply(null, arr);  
		} ,
		 
		/*æå°å¼*/  
		min:function (arr) {  
		    return Math.min.apply(null, arr);  
		} ,
		 
		/*æ±å*/  
		sum:function (arr) {  
			 var arr=ar    
			 return sum=arr.reduce(function(prev, curr, idx, arr){
                  return prev + curr;
             })
		} ,  
		 
		/*å¹³åå¼*/  
		average:function (arr) {  
		    return this.sum(arr)/arr.length  
		},
		remove:function(array,data){
			var index = array.indexOf(data);
			if(index > -1){
				array.splice(index,1);
			}
			return array;
		}
}